<?php

namespace App\Admin\Components;

use App\Models\OwnerItem;
use Dcat\Admin\Widgets\Metrics\Round;

class Building extends Round
{
    protected $building;

    public function __construct($title = null, $icon = null)
    {
        parent::__construct($title, $icon);
    }

    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();
        $building = \App\Models\Building::query()->find(\request()->input('building_id'));
        $this->title($building->name . "的历史收入");

        $rent_ids = [];
        foreach ($building->rooms as $room) {
            if ($room->is_rent) {
                // 查找合同
                $rent_ids[] = $room->id;
            }
        }
        $amount = OwnerItem::query()->whereHas('owner', function ($query) use ($rent_ids) {
            $query->where('ownerable_type', \App\Models\Room::class)->whereIn('ownerable_id', $rent_ids)->where('closed', false);
        })->whereNotNull('pay_at')->sum('total_amount');
        $amount = doubleval($amount);

        if ($amount != 0) {
            $rent_amount = OwnerItem::query()->whereHas('owner', function ($query) use ($rent_ids) {
                $query->where('ownerable_type', \App\Models\Room::class)->whereIn('ownerable_id', $rent_ids)->where('closed', false);
            })->whereNotNull('pay_at')->sum('rent');
            $rent_amount = doubleval($rent_amount);
            $other_amount = $amount - $rent_amount;
            $this->withContent($amount, $rent_amount, $other_amount);
            $this->withChart([100, (int)(($rent_amount * 100) / $amount), (int)(($other_amount * 100) / $amount)]);
            $this->chartTotal('合计', $amount);
        } else {
            $this->withContent(0, 0, 0);
            $this->withChart([0, 0, 0]);
            $this->chartTotal('合计', 0);
        }
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 卡片内容.
     *
     * @param int $finished
     * @param int $pending
     * @param int $rejected
     *
     * @return $this
     */
    public function withContent($finished, $pending, $rejected)
    {
        return $this->content(
            <<<HTML
<div class="col-12 d-flex flex-column flex-wrap text-center" style="max-width: 220px">
    <div class="chart-info d-flex justify-content-between mb-1 mt-2" >
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-primary"></i>
              <span class="text-bold-600 ml-50">总收入</span>
          </div>
          <div class="product-result">
              <span>{$finished}</span>
          </div>
    </div>

    <div class="chart-info d-flex justify-content-between mb-1">
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-warning"></i>
              <span class="text-bold-600 ml-50">租金总收入</span>
          </div>
          <div class="product-result">
              <span>{$pending}</span>
          </div>
    </div>

     <div class="chart-info d-flex justify-content-between mb-1">
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-danger"></i>
              <span class="text-bold-600 ml-50">杂费总收入</span>
          </div>
          <div class="product-result">
              <span>{$rejected}</span>
          </div>
    </div>
</div>
HTML
        );
    }
}
